"""
    Controller for final documetns.
    Also contains recalculation mass action.
"""

from mnemoply import model
from mnemoply.controllers.lookup import LookupController, TableCol, TableLookupDataSource
from mnemoply.controllers.crud import CRUDController
from mnemoply.mass_actions import MassAction
from mnemoply.utils import getDocumentType

class MARecalculate(MassAction):
    """
        Recalculate all selected final documents.
    """

    target_class = model.FinalDocument
    title = _("Recalculate")
    name = "recalculate"

    def call(self, objs):
        for d in objs:
            (document, hours, works_amount, last_topic) = getDocumentType(d.student)

            if document is None:
                continue

            d.documentType = document
            d.hours = hours
            d.works = works_amount
            d.lastTopic = last_topic

class FinalDocumentController(LookupController, CRUDController):
    model_object = model.FinalDocument
    lookup_columns = [
        TableCol(name='student', caption=_('Student')),
        TableCol(name='number', caption=_('#')),
        TableCol(name='year_begin', caption=_('Year begin')),
        TableCol(name='year_end', caption=_('Year end')),
        TableCol(name='hours', caption=_('Hours')),
        TableCol(name='works', caption=_('Works amount')),
        TableCol(name='creation_date', caption=_('Creation date')),
        TableCol(name='defence_date', caption=_('Defence date'), hidden=True),
        TableCol(name='owner', caption=_('Owner')),
        TableCol(name='document_type', caption=_('Document type'), hidden=True),
        TableCol(name='qualification', caption=_('Qualification'), hidden=True),
        TableCol(name='specialization', caption=_('Specialization'), hidden=True),
        TableCol(name='organization', caption=_('Organization'), hidden=True),
        TableCol(name='last_topic', caption=_('Last topic'), hidden=True),
    ]
    edit_fields = [
        'studentID', 'yearBegin', 'yearEnd', 'creationDate', 'defenceDate',
        'ownerID', 'documentTypeID', 'lastTopicID', 'qualificationID',
        'specializationID', 'number', 'organizationID',
        ]
    view_fields = edit_fields + ['hours', 'works']

    def _get_edit_form(self, obj=None):
        form = CRUDController._get_edit_form(self)
        marks = list(model.FinalWorkMark.selectBy(student=obj.student))
        topics = [m.topic for m in marks]
        df = dict([(f.name, f) for f in form.fields])
        df['lastTopicID'].records = topics
        form.fields = df.values()

        return form

    def __init__(self):
        LookupController.__init__(self,
            data_source=TableLookupDataSource('final_document_lookup'),
            columns=self.lookup_columns,
            title=self.model_object.model_title,
            mass_actions_class=self.model_object,
        )
        CRUDController.__init__(self, model_object=self.model_object)
        self.local_menu = []

exposed = FinalDocumentController()
